/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonybeans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author ChauAnh
 */
@Stateless
public class Search implements SearchLocal {

    ConnectDB conDB = new ConnectDB();
    Connection conn;

    @Override
    public HashMap searchByIdAndCasteForGuest(String by, String value) {
        HashMap candidateData = new HashMap();
        try {
            conn = conDB.getConnection();
            String strSql = "SELECT * FROM Candidate WHERE " + by + " LIKE ('%" + value + "%')";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                HashMap hm = new HashMap();
                hm.put("username", rs.getString("username").trim());
                hm.put("first_name", rs.getString("first_name").trim());
                hm.put("middle_name", rs.getString("middle_name").trim());
                hm.put("last_name", rs.getString("last_name").trim());
                hm.put("avatar", rs.getString("avatar").trim());
                candidateData.put(rs.getString("username").trim(), hm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }

        return candidateData;
    }

    @Override
    public HashMap searchByIdAndCasteForMember(String by, String value, String gender) {
        HashMap candidateData = new HashMap();
        try {
            conn = conDB.getConnection();
            String strSql = "SELECT * FROM Candidate WHERE gender != '" + gender + "' AND " + by + " LIKE ('%" + value + "%')";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                HashMap hm = new HashMap();
                hm.put("username", rs.getString("username").trim());
                hm.put("first_name", rs.getString("first_name").trim());
                hm.put("middle_name", rs.getString("middle_name").trim());
                hm.put("last_name", rs.getString("last_name").trim());
                hm.put("avatar", rs.getString("avatar").trim());
                candidateData.put(rs.getString("username").trim(), hm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }

        return candidateData;
    }
}
